package org.dromara.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.demo.domain.bo.JsCaseVarBo;
import org.dromara.demo.domain.vo.JsCaseVarVo;
import org.dromara.demo.domain.JsCaseVar;
import org.dromara.demo.mapper.JsCaseVarMapper;
import org.dromara.demo.service.IJsCaseVarService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用例变量Service业务层处理
 *
 * @author zhu
 * @date 2025-02-18
 */
@RequiredArgsConstructor
@Service
public class JsCaseVarServiceImpl implements IJsCaseVarService {

    private final JsCaseVarMapper baseMapper;

    /**
     * 查询用例变量
     *
     * @param id 主键
     * @return 用例变量
     */
    @Override
    public JsCaseVarVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询用例变量列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 用例变量分页列表
     */
    @Override
    public TableDataInfo<JsCaseVarVo> queryPageList(JsCaseVarBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JsCaseVar> lqw = buildQueryWrapper(bo);
        Page<JsCaseVarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的用例变量列表
     *
     * @param bo 查询条件
     * @return 用例变量列表
     */
    @Override
    public List<JsCaseVarVo> queryList(JsCaseVarBo bo) {
        LambdaQueryWrapper<JsCaseVar> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<JsCaseVar> buildQueryWrapper(JsCaseVarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<JsCaseVar> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getExampleLocation()), JsCaseVar::getExampleLocation, bo.getExampleLocation());
        lqw.like(StringUtils.isNotBlank(bo.getVarName()), JsCaseVar::getVarName, bo.getVarName());
        lqw.eq(StringUtils.isNotBlank(bo.getValue()), JsCaseVar::getValue, bo.getValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMark()), JsCaseVar::getMark, bo.getMark());
        lqw.eq(bo.getCreatedTime() != null, JsCaseVar::getCreatedTime, bo.getCreatedTime());
        return lqw;
    }

    /**
     * 新增用例变量
     *
     * @param bo 用例变量
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JsCaseVarBo bo) {
        JsCaseVar add = MapstructUtils.convert(bo, JsCaseVar.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用例变量
     *
     * @param bo 用例变量
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JsCaseVarBo bo) {
        JsCaseVar update = MapstructUtils.convert(bo, JsCaseVar.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JsCaseVar entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除用例变量信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<JsCaseVarVo> queryByIds(List<String> collect) {
        if (collect.size() > 0) {
            List<JsCaseVar> jsCaseVarVos = baseMapper.selectList(new LambdaQueryWrapper<JsCaseVar>().in(JsCaseVar::getExampleLocation, collect));
            return BeanUtil.copyToList(jsCaseVarVos, JsCaseVarVo.class);
        }
        return null;
    }
}
